﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MvvmHelpers;
using System.Threading.Tasks;
using TabbedTemplate.Models;
using TabbedTemplate.Services;

namespace TabbedTemplate.ViewModels
{
    public class MyPunchesPageViewModel : ViewModelBase
    {

        public const string NoColletion = "暂无任何打卡记录";

        private ObservableRangeCollection<PunchTimeStampParameter>
            _punchInfoCollection;

        public ObservableRangeCollection<PunchTimeStampParameter>
            PunchInfoCollection
        {
            get => _punchInfoCollection;
            set =>
                Set(nameof(PunchInfoCollection), ref _punchInfoCollection,
                    value);
        }


        private string _status;
        public string Status
        {
            get => _status;
            set => Set(nameof(Status), ref _status, value);
        }

        private readonly IPunchInfoStorage _punchInfoStorage;
        private readonly IStoreStorage _storeStorage;
        private readonly IPreferenceStorage _preferenceStorage;
        private readonly IImageService _imageService;
        private readonly IPunchNavigationService _punchNavigationService;

        public MyPunchesPageViewModel(IPunchInfoStorage punchInfoStorage,
            IPreferenceStorage preferenceStorage,
            IStoreStorage storeStorage, IImageService imageService, IPunchNavigationService punchNavigationService)
        {
            _storeStorage = storeStorage;
            _punchInfoStorage = punchInfoStorage;
            _preferenceStorage = preferenceStorage;
            _imageService = imageService;
            _punchNavigationService = punchNavigationService;
        }


        private RelayCommand<PunchTimeStampParameter> _punchInfoTappedCommand;

        public RelayCommand<PunchTimeStampParameter> PunchInfoTappedCommand =>
            _punchInfoTappedCommand ??=
                new RelayCommand<PunchTimeStampParameter>(
                    async punchInfoTimeStampParameter =>
                        await PunchInfoTappedCommandFunction(
                            punchInfoTimeStampParameter));

        public async Task PunchInfoTappedCommandFunction(
            PunchTimeStampParameter punchTimeStampParameter)
        {

            _preferenceStorage.Set("PunchEdit", true);
            var store =
                await _storeStorage.GetStoreByStoreUidAsync(
                    punchTimeStampParameter.PunchInfo.Store_Uid);
            punchTimeStampParameter.Store = store;
            await _punchNavigationService.NavigateToPunchViewPage(
                punchTimeStampParameter);
        }


        private RelayCommand<object> _deletePunchInfoCommand;

        public RelayCommand<object> DeletePunchInfoCommand =>
            _deletePunchInfoCommand ??= new RelayCommand<object>(
                async punchInfoTimeStampParameter =>
                    await DeletePunchInfoCommandFunction(
                        punchInfoTimeStampParameter));

        public async Task DeletePunchInfoCommandFunction(
            object punchTimeStampParameter)
        {

            var a = (PunchTimeStampParameter)punchTimeStampParameter;
            PunchInfoCollection.Remove(a);
            await _punchInfoStorage.DeletePunchInfoByIdAsync(a.PunchInfo);
            _imageService.DeleteFolder(a.PunchInfo.Timestamp);
        }


        private RelayCommand _pageAppearingCommand;

        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ??=
                new RelayCommand(PageAppearingCommandFunction);

        public void PageAppearingCommandFunction()
        {
            if (PunchInfoCollection.Count == 0)
            {
                Status = NoColletion;
            }
        }
    }
}
